import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/sale_bid_marketinfo_odoo_api.dart';
import 'package:flutter_admin/models/odoo/sale_bid_marketinfo.dart';
import 'package:flutter_admin/models/odoo/sale_bid_project.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import '../../../generated/l10n.dart';

class SaleBidMarketinfoEdit extends StatefulWidget {
  final SaleBidProject? saleBidProject;

  SaleBidMarketinfoEdit({this.saleBidProject});

  @override
  _SaleBidMarketinfoEditState createState() => _SaleBidMarketinfoEditState();
}

class _SaleBidMarketinfoEditState extends State<SaleBidMarketinfoEdit> {
  late SaleBidMarketinfo saleBidMarketinfo;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  late List<SelectOptionVO> activeList;

  @override
  void initState() {
    super.initState();
    this.saleBidMarketinfo = SaleBidMarketinfo();
    this.saleBidMarketinfo.projectId = widget.saleBidProject!.id;
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _loadData();
      _loadSelectVO();
    });
  }

  _loadSelectVO() async {
    activeList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '0', label: "否"),
      SelectOptionVO(value: '1', label: "是"),
    ];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            label: '*项目是否内定',
            dataList: activeList,
            value: saleBidMarketinfo.isdesignated,
            onSaved: (v) => {saleBidMarketinfo.isdesignated = v},
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '*客户有意向中标厂家',
            value: saleBidMarketinfo.interestedbidder,
            onSaved: (v) {
              saleBidMarketinfo.interestedbidder = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '*报名厂家名单',
            value: saleBidMarketinfo.registeredlist,
            onSaved: (v) {
              saleBidMarketinfo.registeredlist = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '*客户关系基础',
            value: saleBidMarketinfo.customerrelationship,
            onSaved: (v) {
              saleBidMarketinfo.customerrelationship = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '*质量控制情况',
            value: saleBidMarketinfo.qualitycontrol,
            onSaved: (v) {
              saleBidMarketinfo.qualitycontrol = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '*配套元器件品牌倾向性',
            value: saleBidMarketinfo.orientation,
            onSaved: (v) {
              saleBidMarketinfo.orientation = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '上期投标情况',
            value: saleBidMarketinfo.lasttender,
            onSaved: (v) {
              saleBidMarketinfo.lasttender = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
          CryInput(
            label: '本期竞争对手分析',
            value: saleBidMarketinfo.competitor,
            onSaved: (v) {
              saleBidMarketinfo.competitor = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
          CryInput(
            label: '样品送样情况',
            value: saleBidMarketinfo.evaluationcommittee,
            onSaved: (v) {
              saleBidMarketinfo.evaluationcommittee = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
          CryInput(
            label: '评标委员会名单及关系情况',
            value: saleBidMarketinfo.sampledelivery,
            onSaved: (v) {
              saleBidMarketinfo.sampledelivery = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
          CryInput(
            label: '投标注意事项',
            value: saleBidMarketinfo.precautions,
            onSaved: (v) {
              saleBidMarketinfo.precautions = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
          CryInput(
            label: '报价指导与建议',
            value: saleBidMarketinfo.guidance,
            onSaved: (v) {
              saleBidMarketinfo.guidance = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
          CryInput(
            label: '取投标书的形式',
            value: saleBidMarketinfo.tenderform,
            onSaved: (v) {
              saleBidMarketinfo.tenderform = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
          CryInput(
            label: '其他',
            value: saleBidMarketinfo.others,
            onSaved: (v) {
              saleBidMarketinfo.others = v;
            },
            // validator: (v) {
            //   return v!.isEmpty ? S.of(context).required : null;
            // },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            SaleBidMarketinfoOdooApi.saveOrUpdate(saleBidMarketinfo.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text('市场信息'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
  }

  _loadData() async {
    ResponseBodyApi responseBodyApi = await SaleBidMarketinfoOdooApi.page(
        data: {"domainList": saleBidMarketinfo.toDomainList()});
    if (responseBodyApi.success!) {
      saleBidMarketinfo = responseBodyApi.data.length != 0
          ? SaleBidMarketinfo.fromMap(responseBodyApi.data[0])
          : SaleBidMarketinfo();
      saleBidMarketinfo.projectId = widget.saleBidProject!.id;
      setState(() {});
    }
  }
}
